<?php
namespace app\admin\logic;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;

class SpaceRemarkLogic extends BaseLogic {

    /**
     * 获取沟通备注查询条件
     * @param $branchid
     * @param $floorid
     * @param $work_type
     * @param $username
     * @param $start_time
     * @param $end_time
     * @return array
     */
    public function get_search_sr_maps($branchid,$floorid,$number,$work_type,$username,$start_time,$end_time){
        $maps = [];
        $maps[] = ['remark','<>',''];

        if($branchid != -1){
            $maps[] = ['branchid','=',$branchid];
        }

        if(!empty($number)){
            $ws_maps = [];
            $ws_maps[] = ['number','=',$number];
            $wsids = model('work_station')->get_column($ws_maps,'id');
            $maps[] = ['wsid','in',$wsids];
        }

        if($work_type != -1){
            $ws_maps = [];
            $ws_maps[] = ['work_type','=',$work_type];
            $wsids = model('work_station')->get_column($ws_maps,'id');
            $maps[] = ['wsid','in',$wsids];
        }

        if(!empty($username)){
            $u_maps = [];
            $u_maps[] = ['username','like','%' . $username . '%'];
            $openids = model('user')->get_column($u_maps,'openid');
            $maps[] = ['openid','in',$openids];
        }

        if($floorid != -1){
            $maps[] = ['floorid','=',$floorid];
        }

        if(!empty($start_time) && !empty($end_time)){
            $maps[] = ['c_time','between',[$start_time,$end_time]];
        }
        return $maps;
    }

    /**
     * 获取沟通备注
     * @param $params
     * @return mixed
     */
    public function get_sr($params){
        $maps = $this->get_search_sr_maps($params['branchid'],$params['floorid'],$params['number'],$params['work_type'],$params['username'],$params['start_time'],$params['end_time']);
        $limit = ($params['page'] - 1) * $params['rows'];

        $list = model('cp_region_ws')->get_list($maps,'id,openid,branchid,floorid,regionid,wsid,remark,c_time','id desc','',$limit,$params['rows']);
        foreach($list as $k => $v){
            //查找工位
            $ws_maps = [];
            $ws_maps[] = ['id','=',$v['wsid']];
            $ws_detail = model('work_station')->get_detail($ws_maps,'id,number,work_type');
            $list[$k]['number'] = !empty($ws_detail) ? $ws_detail['number'] : '';
            $list[$k]['work_type_zw'] = !empty($ws_detail) ? $ws_detail->work_type_name : '';

            //查找区域
            $r_maps = [];
            $r_maps[] = ['id','=',$v['regionid']];
            $r_detail = model('region')->get_detail($r_maps,'id,floorid,name');
            $list[$k]['region_zw'] = !empty($r_detail) ? $r_detail['name'] : '';

            //查找楼层
            $f_maps = [];
            $f_maps[] = ['id','=',$r_detail['floorid']];
            $f_detail = model('floor')->get_detail($f_maps,'id,branchid,name');
            $list[$k]['floor_zw'] = !empty($f_detail) ? $f_detail['name'] : '';

            //查找分部
            $b_maps = [];
            $b_maps[] = ['id','=',$f_detail['branchid']];
            $b_detail = model('branch')->get_detail($b_maps,'id,name');
            $list[$k]['branch_zw'] = !empty($b_detail) ? $b_detail['name'] : '';

            //查找用户
            $u_maps = [];
            $u_maps[] = ['openid','=',$v['openid']];
            $u_detail = model('user')->get_detail($u_maps,'id,username');
            $list[$k]['username'] = !empty($u_detail) ? $u_detail['username'] : '';
        }

        $total_num = model('cp_region_ws')->get_count($maps,'id');

        $data['list'] = $list;
        $data['total_num'] = $total_num;
        return $this->get_response(1,'OK',$data);
    }

    /**
     * 生成沟通备注excel
     * @param $ExcelName
     * @param $data
     * @return bool
     */
    public function gen_sr_excel($ExcelName,$data){
        if(count($data[0]) > 26){
            return $this->get_response(0,'导出excel最大列数为26，请缩减数据列数');
        }
        $spreadsheet = new Spreadsheet();
        $sheet = $spreadsheet->getActiveSheet();
        for($row = 1;$row <= count($data);$row++){
            $index = 0;
            foreach($data[$row-1] as $k => $v) {
                $col = chr($index + 65);
                $cell = ($col . $row);
                $sheet->setCellValue($cell, $v);
                $index++;
            }
        }

        header('Content-Type: application/vnd.ms-excel;charset=utf8-general-ci');
        header('Content-Disposition: attachment;filename='.$ExcelName);
        header('Cache-Control: max-age=0');

        $writer = new Xlsx($spreadsheet);
        $writer->save('php://output');
        //$writer->save($ExcelName);
        return true;
    }
}